package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 *  山东电视台 解析类
 * 
 * @author Chase
 * 
 */
public class IqiluVideoAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
//		if(url.indexOf("ad") != -1 || url.indexOf("public") != -1 || url.indexOf("hosts") != -1) {
//			return bRet;
//		}
		String[] regex = {
				"http://v.iqilu.com/.*/[0-9]{4,6}/[0-9]{2,4}/[0-9].*.html",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}	

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url = urlMeta.getUrl(); 
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
			System.err.println("不符合规则");
		}

		String html = urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title = null;
		String dateStr = null;
		String desc = null;
		Long dateLong = null;
		String commentNum = null;
		String clickNum = null;
		String author = null;
		

		Document doc = Jsoup.parse(html);
		
		title = doc.select("h1").text();// 标题
		if(StringUtils.isBlank(title)){
			title = doc.select(".big_tit").text();
		}
		
		dateStr = doc.select(".vtitle,#pubtime_baidu").text();
		dateLong = DateUtils.matchDate(dateStr);// 时间
		
	
		desc = doc.select("div#jsxx").text(); //描述
		if(StringUtils.isBlank(desc)){
			desc=doc.select("h3").text();
		}
		author = StringUtils.substringAfter(doc.select(".info_b").text(),"来源：");
		if(!StringUtils.isNullOrEmpty(desc)) {
			desc = desc.length()>1000?desc.substring(0, 1000):desc;//小于1000字符的描述
		}
		
		commentNum = doc.select("div#dianboshu").text(); //点击数
		
		NewsMeta meta = new NewsMeta();
		meta.setTitle(title);
		meta.setContent(desc);
		meta.setUrl(url);
		meta.setType(2);
		meta.setClickNum(NumberUtils.parseInt(clickNum));
		meta.setCommentNum(NumberUtils.parseInt(commentNum));
		meta.setDate(dateLong);

		/**
		 * 解析用于更新的地址
		 */
//		String author=null;
		Elements select = doc.select("html body div.wrapper div.con_lft div#nr_left div.wjlaiyuan ul li");
		if(select!=null && select.size()!=0){
			author=select.get(0).text();
			if(author!=null && author.indexOf("来源：")!=-1){
				author=StringUtils.substringAfter(author, "来源：");
			}
		}
		meta.setAuthor(author);
		return meta;
	}
	

	
	public NewsMeta Update(NewsMeta meta) {
		
		return null;
	}


	
}
